package com.rams.jhms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.rams.jhms.dto.ArtistDTO;
import com.rams.jhms.logging.BaseLogger;

public class DerbyArtistDAO extends DerbyBaseDAO{
	public ArtistDTO getArtistById(int id) {
		Connection con = getConnection();
        PreparedStatement st = null;
        ResultSet rs = null;

        ArtistDTO art = null;
        try {
			st = con.prepareStatement("SELECT * FROM artist WHERE id=?");
			st.setInt(0, id);
	        rs = st.executeQuery();
	        if (rs.next()) {
	        	art = new ArtistDTO();
	        	art.setId(id);
	        	art.setName(rs.getString("name"));
	        	art.setFilePath(rs.getString("path"));
	        }
        } catch (SQLException sqle) {
        	BaseLogger.error(this, sqle);
        } catch (Exception e) {
        	BaseLogger.error(this, e);
		} finally {
			close(con, st, rs);
		}
		
		return art;
	}
	
	public void addAtrist(ArtistDTO newArtist) {
		
	}
	
	public int getArtistCount() {
		Connection con = getConnection();
        PreparedStatement st = null;
        ResultSet rs = null;

        int count = 0;
        try {
			st = con.prepareStatement("SELECT COUNT(id) AS count FROM artists");
	        rs = st.executeQuery();
	        if (rs.next()) {
	        	
	        }
	        while (rs.next()) {
	        	rs.getInt("count");
	        }
        } catch (SQLException sqle) {
        	BaseLogger.error(this, sqle);
        } catch (Exception e) {
        	BaseLogger.error(this, e);
		} finally {
			close(con, st, rs);
		}
		
		return count;
	}

	public List<ArtistDTO> findArtistByName(String name) {
		Connection con = getConnection();
        PreparedStatement st = null;
        ResultSet rs = null;

        ArrayList<ArtistDTO> artList = new ArrayList<ArtistDTO>();
        try {
			st = con.prepareStatement("SELECT * FROM artist WHERE name LIKE ?");
			st.setString(1, name);
	        rs = st.executeQuery();
	        while (rs.next()) {
	        	ArtistDTO art = new ArtistDTO();
	        	art.setId(rs.getInt("id"));
	        	art.setName(rs.getString("name"));
	        	art.setFilePath(rs.getString("path"));
	        	artList.add(art);
	        }
        } catch (SQLException sqle) {
        	BaseLogger.error(this, sqle);
        } catch (Exception e) {
        	BaseLogger.error(this, e);
		} finally {
			close(con, st, rs);
		}
		
		return artList;
	}
	
	public List<ArtistDTO> getArtistsList() {
		Connection con = getConnection();
        PreparedStatement st = null;
        ResultSet rs = null;

        ArrayList<ArtistDTO> artList = new ArrayList<ArtistDTO>();
        try {
			st = con.prepareStatement("SELECT * FROM artist");
	        rs = st.executeQuery();
	        while (rs.next()) {
	        	ArtistDTO art = new ArtistDTO();
	        	art.setId(rs.getInt("id"));
	        	art.setName(rs.getString("name"));
	        	art.setFilePath(rs.getString("path"));
	        	artList.add(art);
	        }
        } catch (SQLException sqle) {
        	BaseLogger.error(this, sqle);
        } catch (Exception e) {
        	BaseLogger.error(this, e);
		} finally {
			close(con, st, rs);
		}
		
		return artList;
	}
}
